<div id="partitions" class="card mb-3">
    <div class="card-header">
      {{ t('Partitions') }}
      {{ show_mysql_docu('partitioning') }}
    </div>

    <div class="card-body">
        {% if partitions is not defined or partitions is empty %}
            {{ t('No partitioning defined!')|notice }}
        {% else %}
            <p>
                {{ t('Partitioned by:') }}
                <code>{{ partition_method }}({{ partition_expression }})</code>
            </p>
            {% if has_sub_partitions %}
                <p>
                    {{ t('Sub partitioned by:') }}
                    <code>{{ sub_partition_method }}({{ sub_partition_expression }})</code>
                <p>
            {% endif %}
            <table class="table table-striped table-hover table-sm">
                <thead>
                    <tr>
                        <th colspan="2">#</th>
                        <th>{{ t('Partition') }}</th>
                        {% if has_description %}
                            <th>{{ t('Expression') }}</th>
                        {% endif %}
                        <th>{{ t('Rows') }}</th>
                        <th>{{ t('Data length') }}</th>
                        <th>{{ t('Index length') }}</th>
                        <th>{{ t('Comment') }}</th>
                        <th colspan="{{ range_or_list ? '7' : '6' }}">
                            {{ t('Action') }}
                        </th>
                    </tr>
                </thead>
                <tbody>
                {% for partition in partitions %}
                    <tr class="noclick{{ has_sub_partitions ? ' table-active' }}">
                        {% if has_sub_partitions %}
                            <td>{{ partition.getOrdinal() }}</td>
                            <td></td>
                        {% else %}
                            <td colspan="2">{{ partition.getOrdinal() }}</td>
                        {% endif %}
                        <th>{{ partition.getName() }}</th>
                        {% if has_description %}
                            <td>
                                <code>
                                    {{- partition.getExpression() -}}
                                    {{- partition.getMethod() == 'LIST' ? ' IN (' : ' < ' -}}
                                    {{- partition.getDescription() -}}
                                    {{- partition.getMethod() == 'LIST' ? ')' -}}
                                </code>
                            </td>
                        {% endif %}
                        <td class="value">{{ partition.getRows() }}</td>
                        <td class="value">
                            {% set data_length = format_byte_down(
                                partition.getDataLength(),
                                3,
                                1
                            ) %}
                            <span>{{ data_length[0] }}</span>
                            <span class="unit">{{ data_length[1] }}</span>
                        </td>
                        <td class="value">
                            {% set index_length = format_byte_down(
                                partition.getIndexLength(),
                                3,
                                1
                            ) %}
                            <span>{{ index_length[0] }}</span>
                            <span class="unit">{{ index_length[1] }}</span>
                        </td>
                        <td>{{ partition.getComment() }}</td>

                        <td>
                          <a id="partition_action_ANALYZE" class="ajax" href="{{ url('/table/partition/analyze') }}" data-post="{{ get_common({
                            'db': db,
                            'table': table,
                            'partition_name': partition.getName(),
                          }, '', false) }}">
                            {{ get_icon('b_search', t('Analyze')) }}
                          </a>
                        </td>

                        <td>
                          <a id="partition_action_CHECK" class="ajax" href="{{ url('/table/partition/check') }}" data-post="{{ get_common({
                            'db': db,
                            'table': table,
                            'partition_name': partition.getName(),
                          }, '', false) }}">
                            {{ get_icon('eye', t('Check')) }}
                          </a>
                        </td>

                        <td>
                          <a id="partition_action_OPTIMIZE" class="ajax" href="{{ url('/table/partition/optimize') }}" data-post="{{ get_common({
                            'db': db,
                            'table': table,
                            'partition_name': partition.getName(),
                          }, '', false) }}">
                            {{ get_icon('normalize', t('Optimize')) }}
                          </a>
                        </td>

                        <td>
                          <a id="partition_action_REBUILD" class="ajax" href="{{ url('/table/partition/rebuild') }}" data-post="{{ get_common({
                            'db': db,
                            'table': table,
                            'partition_name': partition.getName(),
                          }, '', false) }}">
                            {{ get_icon('s_tbl', t('Rebuild')) }}
                          </a>
                        </td>

                        <td>
                          <a id="partition_action_REPAIR" class="ajax" href="{{ url('/table/partition/repair') }}" data-post="{{ get_common({
                            'db': db,
                            'table': table,
                            'partition_name': partition.getName(),
                          }, '', false) }}">
                            {{ get_icon('b_tblops', t('Repair')) }}
                          </a>
                        </td>

                        <td>
                          <a id="partition_action_TRUNCATE" class="ajax" href="{{ url('/table/partition/truncate') }}" data-post="{{ get_common({
                            'db': db,
                            'table': table,
                            'partition_name': partition.getName(),
                          }, '', false) }}">
                            {{ get_icon('b_empty', t('Truncate')) }}
                          </a>
                        </td>

                        {% if range_or_list %}
                          <td>
                            <a id="partition_action_DROP" class="ajax" href="{{ url('/table/partition/drop') }}" data-post="{{ get_common({
                              'db': db,
                              'table': table,
                              'partition_name': partition.getName(),
                            }, '', false) }}">
                              {{ get_icon('b_drop', t('Drop')) }}
                            </a>
                          </td>
                        {% endif %}

                        {% if has_sub_partitions %}
                            {% for sub_partition in partition.getSubPartitions() %}
                                <tr class="noclick">
                                    <td></td>
                                    <td>{{ sub_partition.getOrdinal() }}</td>
                                    <td>{{ sub_partition.getName() }}</td>
                                    {% if has_description %}
                                        <td></td>
                                    {% endif %}
                                    <td class="value">{{ sub_partition.getRows() }}</td>
                                    <td class="value">
                                        {% set data_length = format_byte_down(
                                            sub_partition.getDataLength(),
                                            3,
                                            1
                                        ) %}
                                        <span>{{ data_length[0] }}</span>
                                        <span class="unit">{{ data_length[1] }}</span>
                                    </td>
                                    <td class="value">
                                        {% set index_length = format_byte_down(
                                            sub_partition.getIndexLength(),
                                            3,
                                            1
                                        ) %}
                                        <span>{{ index_length[0] }}</span>
                                        <span class="unit">{{ index_length[1] }}</span>
                                    </td>
                                    <td>{{ sub_partition.getComment() }}</td>
                                    <td colspan="{{ range_or_list ? '7' : '6' }}"></td>
                                </tr>
                            {% endfor %}
                        {% endif %}
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        {% endif %}
    </div>
    <div class="card-footer d-print-none">
        <form action="{{ url('/table/structure/partitioning') }}" method="post">
            {{ get_hidden_inputs(db, table) }}

            {% if partitions is not defined or partitions is empty %}
                <input class="btn btn-secondary" type="submit" value="{{ t('Partition table') }}">
            {% else %}
                {{ link_or_button(
                  url('/sql'),
                  {
                    'db': db,
                    'table': table,
                    'sql_query': 'ALTER TABLE ' ~ backquote(table) ~ ' REMOVE PARTITIONING'
                  },
                  t('Remove partitioning'), {
                    'class': 'btn btn-secondary ajax',
                    'id': 'remove_partitioning'
                }) }}
                <input class="btn btn-secondary" type="submit" value="{{ t('Edit partitioning') }}">
            {% endif %}
        </form>
    </div>
</div>
